#include <algorithm>
#include <cstdint>
#include <iostream>
#include <map>
#include <utility>
#include <vector>

using ll = int64_t;
using std::cin, std::cout;

const ll maxn=5e5+5, inf{1l<<(sizeof(inf)*8-4)};
ll n, k, kd[maxn], mkd[maxn];

int main(){
    std::iostream::sync_with_stdio(false), cin.tie(nullptr), cout.tie(nullptr);

    cin>>n>>k;
    std::fill(mkd, mkd+1+k, -inf);
    std::vector<std::vector<ll>> adj(n+1, std::vector<ll>(n+1,inf));
    for(ll i{1};i<=n;i++){
        cin>>kd[i];
    }
    for(ll i{1};i<n;i++){
        ll u,v,w;
        cin>>u>>v>>w;
        adj[u][v]=adj[v][u]=std::min({
            adj[u][v],
            adj[v][u],
            w
        });
    }
    for(ll k{1};k<=n;k++){
        for(ll i{1};i<=n;i++){
            for(ll j{1};j<=n;j++){
                adj[i][j]=std::min(
                    adj[i][j],
                    adj[i][k]+adj[k][j]
                );
            }
        }
    }
    for(ll i{1};i<=n;i++){
        for(ll j{1};j<=n;j++){
            if(i!=j && kd[i]==kd[j]){
                mkd[kd[i]]=std::max(mkd[kd[i]], adj[i][j]);
            }
        }
    }
    for(ll i{1};i<=k;i++){
        cout<<(mkd[i]==inf?-1:mkd[i])<<'\n';
    }
}